package com.haotian.date;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtils {
	private static Calendar cal = Calendar.getInstance();

	static {
		/**
		 * 每次都要调用这个初始化当前的 Calendar 对象的时间 避免重复创建 消耗过多内存和CPU
		 */
		cal.setTimeInMillis(System.currentTimeMillis());
	}

	public static void init(long timeMillis) {
		cal.setTimeInMillis(timeMillis);
	}

	/**
	 * 获得月份
	 * 
	 * @return
	 */
	public static long getMonth() {
		return cal.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获得月份中第几天
	 * 
	 * @return
	 */
	public static long getDay() {
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获得小时
	 * 
	 * @return
	 */
	public static long getHour() {
		return cal.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获得分钟
	 * 
	 * @return
	 */
	public static long getMinute() {
		return cal.get(Calendar.MINUTE);
	}

	public static long getYear() {
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 获得秒
	 * 
	 * @return
	 */
	public static long getSecond() {
		return cal.get(Calendar.SECOND);
	}

	public static String formatData2String(String pattern, long timeMillis) {
		return new SimpleDateFormat(pattern).format(timeMillis);
	}

}
